home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter10 / isohex10_2 / isohex10_2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-20  |  7.6 KB  |  335 lines

  1. /*****************************************************************************
  2. IsoHex10_2.cpp
  3. Ernest S. Pazera
  4. 30JUN2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. Art by Ari Feldman
  10. *****************************************************************************/
  11.  
  12. //////////////////////////////////////////////////////////////////////////////
  13. //INCLUDES
  14. //////////////////////////////////////////////////////////////////////////////
  15. #define WIN32_LEAN_AND_MEAN  
  16.  
  17. #include <windows.h>
  18. #include "GDICanvas.h"
  19. #include "ddraw.h"
  20. #include "DDFuncs.h"
  21. #include "TileSet.h"
  22.    
  23. //////////////////////////////////////////////////////////////////////////////
  24. //DEFINES
  25. //////////////////////////////////////////////////////////////////////////////
  26. //name for our window class
  27. #define WINDOWCLASS "ISOHEX10"
  28. //title of the application
  29. #define WINDOWTITLE "IsoHex 10-2, with art by Ari Feldman"
  30.  
  31. //////////////////////////////////////////////////////////////////////////////
  32. //PROTOTYPES
  33. //////////////////////////////////////////////////////////////////////////////
  34. bool Prog_Init();//game data initalizer
  35. void Prog_Loop();//main game loop
  36. void Prog_Done();//game clean up
  37.  
  38. //////////////////////////////////////////////////////////////////////////////
  39. //GLOBALS
  40. //////////////////////////////////////////////////////////////////////////////
  41. HINSTANCE hInstMain=NULL;//main application handle
  42. HWND hWndMain=NULL;//handle to our main window
  43.  
  44. //directdraw
  45. LPDIRECTDRAW7 lpdd=NULL;
  46. LPDIRECTDRAWSURFACE7 lpddsMain=NULL;
  47. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  48. LPDIRECTDRAWCLIPPER lpddclip=NULL;
  49. CTileSet tsCaveMan[2];
  50. DWORD dwCaveManFrame=0;
  51. DWORD dwCaveManFace=0;//0==right, 1==left
  52. DWORD dwCaveManPosition=400;
  53. bool MoveLeft=false;
  54. bool MoveRight=false;
  55.  
  56. //////////////////////////////////////////////////////////////////////////////
  57. //WINDOWPROC
  58. //////////////////////////////////////////////////////////////////////////////
  59. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  60. {
  61.     //which message did we get?
  62.     switch(uMsg)
  63.     {
  64.     case WM_KEYDOWN:
  65.         {
  66.             //on escape, destroy main window
  67.             if(wParam==VK_ESCAPE)
  68.             {
  69.                 DestroyWindow(hWndMain);
  70.             }
  71.  
  72.             //movement keys
  73.             if(wParam==VK_LEFT)
  74.             {
  75.                 MoveLeft=true;
  76.             }
  77.             if(wParam==VK_RIGHT)
  78.             {
  79.                 MoveRight=true;
  80.             }
  81.  
  82.             return(0);//handled
  83.         }break;
  84.     case WM_KEYUP:
  85.         {
  86.             //movement keys
  87.             if(wParam==VK_LEFT)
  88.             {
  89.                 MoveLeft=false;
  90.             }
  91.             if(wParam==VK_RIGHT)
  92.             {
  93.                 MoveRight=false;
  94.             }
  95.             return(0);//handled
  96.         }break;
  97.     case WM_DESTROY://the window is being destroyed
  98.         {
  99.  
  100.             //tell the application we are quitting
  101.             PostQuitMessage(0);
  102.  
  103.             //handled message, so return 0
  104.             return(0);
  105.  
  106.         }break;
  107.     case WM_PAINT://the window needs repainting
  108.         {
  109.             //a variable needed for painting information
  110.             PAINTSTRUCT ps;
  111.             
  112.             //start painting
  113.             HDC hdc=BeginPaint(hwnd,&ps);
  114.  
  115.             /////////////////////////////
  116.             //painting code would go here
  117.             /////////////////////////////
  118.  
  119.             //end painting
  120.             EndPaint(hwnd,&ps);
  121.                         
  122.             //handled message, so return 0
  123.             return(0);
  124.         }break;
  125.     }
  126.  
  127.     //pass along any other message to default message handler
  128.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  129. }
  130.  
  131.  
  132. //////////////////////////////////////////////////////////////////////////////
  133. //WINMAIN
  134. //////////////////////////////////////////////////////////////////////////////
  135. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  136. {
  137.     //assign instance to global variable
  138.     hInstMain=hInstance;
  139.  
  140.     //create window class
  141.     WNDCLASSEX wcx;
  142.  
  143.     //set the size of the structure
  144.     wcx.cbSize=sizeof(WNDCLASSEX);
  145.  
  146.     //class style
  147.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  148.  
  149.     //window procedure
  150.     wcx.lpfnWndProc=TheWindowProc;
  151.  
  152.     //class extra
  153.     wcx.cbClsExtra=0;
  154.  
  155.     //window extra
  156.     wcx.cbWndExtra=0;
  157.  
  158.     //application handle
  159.     wcx.hInstance=hInstMain;
  160.  
  161.     //icon
  162.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  163.  
  164.     //cursor
  165.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  166.  
  167.     //background color
  168.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  169.  
  170.     //menu
  171.     wcx.lpszMenuName=NULL;
  172.  
  173.     //class name
  174.     wcx.lpszClassName=WINDOWCLASS;
  175.  
  176.     //small icon
  177.     wcx.hIconSm=NULL;
  178.  
  179.     //register the window class, return 0 if not successful
  180.     if(!RegisterClassEx(&wcx)) return(0);
  181.  
  182.     //create main window
  183.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  184.  
  185.     //error check
  186.     if(!hWndMain) return(0);
  187.  
  188.     //if program initialization failed, then return with 0
  189.     if(!Prog_Init()) return(0);
  190.  
  191.     //message structure
  192.     MSG msg;
  193.  
  194.     //message pump
  195.     for(;;)    
  196.     {
  197.         //look for a message
  198.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  199.         {
  200.             //there is a message
  201.  
  202.             //check that we arent quitting
  203.             if(msg.message==WM_QUIT) break;
  204.             
  205.             //translate message
  206.             TranslateMessage(&msg);
  207.  
  208.             //dispatch message
  209.             DispatchMessage(&msg);
  210.         }
  211.  
  212.         //run main game loop
  213.         Prog_Loop();
  214.     }
  215.     
  216.     //clean up program data
  217.     Prog_Done();
  218.  
  219.     //return the wparam from the WM_QUIT message
  220.     return(msg.wParam);
  221. }
  222.  
  223. //////////////////////////////////////////////////////////////////////////////
  224. //INITIALIZATION
  225. //////////////////////////////////////////////////////////////////////////////
  226. bool Prog_Init()
  227. {
  228.     //create IDirectDraw7
  229.     lpdd=LPDD_Create(hWndMain,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
  230.  
  231.     //set display mode
  232.     lpdd->SetDisplayMode(800,600,16,0,0);
  233.  
  234.     //create primary surface
  235.     lpddsMain=LPDDS_CreatePrimary(lpdd,1);
  236.  
  237.     //get back buffer
  238.     lpddsBack=LPDDS_GetSecondary(lpddsMain);
  239.  
  240.     //clear out back buffer
  241.     DDBLTFX ddbltfx;
  242.     DDBLTFX_ColorFill(&ddbltfx,0);
  243.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  244.  
  245.     //create clipper
  246.     HRGN hrgn=CreateRectRgn(0,0,800,600);
  247.     lpddclip=LPDDCLIP_Create(lpdd,hrgn);
  248.     DeleteObject(hrgn);
  249.  
  250.     //attach clipper to back buffer
  251.     lpddsBack->SetClipper(lpddclip);
  252.  
  253.     //load in tilesets
  254.     tsCaveMan[0].Load(lpdd,"IsoHex10_2-1.bmp");
  255.     tsCaveMan[1].Load(lpdd,"IsoHex10_2-2.bmp");
  256.  
  257.  
  258.     return(true);//return success
  259. }
  260.  
  261. //////////////////////////////////////////////////////////////////////////////
  262. //CLEANUP
  263. //////////////////////////////////////////////////////////////////////////////
  264. void Prog_Done()
  265. {
  266.     //release clipper
  267.     LPDDCLIP_Release(&lpddclip);
  268.  
  269.     //destroy primary surface
  270.     LPDDS_Release(&lpddsMain);
  271.  
  272.     //destroy IDirectDraw7
  273.     LPDD_Release(&lpdd);
  274. }
  275.  
  276. //////////////////////////////////////////////////////////////////////////////
  277. //MAIN GAME LOOP
  278. //////////////////////////////////////////////////////////////////////////////
  279. void Prog_Loop()
  280. {
  281.     //start timer
  282.     DWORD dwTimeStart=GetTickCount();
  283.  
  284.     //clear out back buffer
  285.     DDBLTFX ddbltfx;
  286.     DDBLTFX_ColorFill(&ddbltfx,0);
  287.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  288.  
  289.     //put tile
  290.     tsCaveMan[dwCaveManFace].PutTile(lpddsBack,dwCaveManPosition,300,dwCaveManFrame);
  291.  
  292.     //move
  293.     if(MoveLeft^MoveRight)
  294.     {
  295.         if(MoveLeft)
  296.         {
  297.             //moving left
  298.             dwCaveManFace=1;
  299.  
  300.             //update position
  301.             dwCaveManPosition+=796;
  302.             dwCaveManPosition%=800;
  303.  
  304.             //update animation frame
  305.             dwCaveManFrame+=1;
  306.             dwCaveManFrame%=7;
  307.         }
  308.         else
  309.         {
  310.             //moving right
  311.             dwCaveManFace=0;
  312.  
  313.             //update position
  314.             dwCaveManPosition+=4;
  315.             dwCaveManPosition%=800;
  316.  
  317.             //update animation frame
  318.             dwCaveManFrame+=1;
  319.             dwCaveManFrame%=7;
  320.         }
  321.     }
  322.     else
  323.     {
  324.         //standing
  325.         dwCaveManFrame=7;
  326.     }
  327.  
  328.     //flip
  329.     lpddsMain->Flip(NULL,DDFLIP_WAIT);
  330.  
  331.     //lock to 15 FPS
  332.     while(GetTickCount()-dwTimeStart<66);
  333. }
  334.  
  335.